package com.aurora.base.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class JsonUtil {

	static String[] staticExcludes = new String[]{};

	public static org.json.JSONArray ListToJon(List list, String[] Excludes) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class,
				new JsonDateValueUtil());
		jsonConfig.setIgnoreDefaultExcludes(false); // 设置默认忽略

		List<String> exList = new ArrayList<>();
		Collections.addAll(exList, staticExcludes);
		if (Excludes != null) {
			Collections.addAll(exList, Excludes);
		}

		jsonConfig.setExcludes(exList.toArray(new String[exList.size()]));
		JSONArray jsonArray = new JSONArray().fromObject(list, jsonConfig);
		try {
			return new org.json.JSONArray(jsonArray.toString());
		} catch (Exception e) {
			return null;
		}
	}

	public static org.json.JSONObject BeanToJon(Object o, String[] Excludes) {
		try {
			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(Date.class,
					new JsonDateValueUtil());
			config.setIgnoreDefaultExcludes(false); // 设置默认忽略

			List<String> exList = new ArrayList<>();
			Collections.addAll(exList, staticExcludes);
			if (Excludes != null) {
				Collections.addAll(exList, Excludes);
			}
			config.setExcludes(exList.toArray(new String[exList.size()]));// 除去emps属性
			JSONObject json = JSONObject.fromObject(o, config);
			return new org.json.JSONObject(json.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
